$(function() {
Request.init();
    $("#Search").button({
        text: true,
        icons: {
            primary: "ui-icon-search"
        }
    });

    $(window).hashchange(Request._Search);
});

var Request =
{
    init: function() {
        //binding events.
        $('#content a[delsn]').live("click", function() {
            Request.delConfirm($(this).attr("delsn"));
        });
        $('#content a[dsn]').live("click", function() {
            Request.SearchBySN($(this).attr("dsn"));
        });
        $('#content a[memsn]').live("click", function(e) {
            Order.GetOrderForm($(this).attr("memsn"), e);
        });
        $('#pageing').change(Request.Search);
        $("#Search").click(function() {
            $("#pageing").val('1');
            Request.Search();
        });
        //set default values.
        utility.formatPaging($('#pageing'), 1, 1);
        //set init action
        if (utility.getObjFromHash() != null) {
            Request._Search();
        }
        //set default event.
        Utils.textBoxsOnEnter(function() { $("#Search").trigger('click'); });
    }
    , delConfirm: function(sn) {
        var objfun = function() {
            Request.doDel(sn);
        }
        utility.showPopUp(m.Msg0003, 3, objfun);
    }
    , doDel: function(x) {
        var param = { sn: x };
        var cb = function() { window.parent.location.reload(); }
        utility.ajaxext("RequestService/Delete", param, "POST", cb);
    }
    , SearchBySN: function(uplinesn) {
        $('#uplinesn').val(uplinesn);
        this.Search();
    }
    , makeBreadCrumbs: function(level) {
        var breadcs = '';
        var listing = $('#Requestbclisting').html();
        if (level >= 3) {
            breadcs = ' > ' + l.RequestPosition3;
        } else {
            switch (level) {
                case 2:
                    breadcs = ' > ' + l.RequestPosition2 + breadcs;
                case 1:
                    breadcs = ' > ' + l.RequestPosition1 + breadcs;
            }
        }
        $('#Requestbreadcrumbs').html(breadcs + listing);
    }
    , Search: function() {
        var param = { page: $('#pageing').val(), searchkey: $('#searchkey').val(), uplinesn: $('#uplinesn').val(), dtfrom: $('#dtfrom').val(), dtto: $('#dtto').val() };
        utility.setObjToHash(param);
    }
    , _Search: function() {
        var param = utility.addPageToPara(utility.getObjFromHash());
        $('#uplinesn').val(param.uplinesn);
        utility.service('RequestService/SearchRequest', param, 'POST', function(data) {
            alert(data.code);
            if (data.code > 0) {
                if (data.d.length > 0) {
                    var currentpage = (parseInt(param['page'], 10) < data.tp) ? parseInt(param['page'], 10) : data.tp;
                    var templatepara = { 'pList': data.d, 'g': g, 'pg': currentpage };
                    utility.template("Request/MAlisting.html", function(template) {
                        $('#DataListing').html(template.process(templatepara));
                        utility.formatPaging($('#pageing'), data.tp, currentpage);
                        PageInitial.InitialPopupDiv();
                    }, "DataListing");
                } else {
                    $('#DataListing').html('<tr><td colspan="9" class="align-center">' + data.msg + '</td></tr>');
                }
            } else {
                utility.showPopUp(data.msg, 1);
            }
        });
    }
};
